<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Order as MOrder;

class Order extends Controller
{
    public function index(Request $request)
    {
        $page = $request->input('page');
        $query = DB::table('orders')
            ->leftJoin('customers', 'customers.id', '=', 'orders.user_id')
            ->leftJoin('shop_car', 'shop_car.id', '=', 'orders.shop_car_id')
            ->select('orders.*', 'customers.nickname', 'customers.address', 'customers.phone', 'shop_car.money', 'shop_car.number');
        if ($filter = $request->input('filter.id')) {
            $query->where('orders.id', $filter);
        }
        if ($filter = $request->input('filter.status')) {
            $query->where('orders.status', $filter);
        }
        return $query->paginate($page['size'], ['*'], 'page', $page['current']);
    }

    //编辑订单状态
    public function update(Request $request, $id)
    {
        $order = MOrder::find($id);
        assert($order);
        if ($order->status == 1){
            $order->status = 2;
        }else if ($order->status == 2){
            $order->status = 3;
        }
        $order->save();
        return response()->json($order->status);
    }
}
